package com.example.mcdonaldapplication.fragment

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.mcdonaldapplication.R
import com.example.mcdonaldapplication.activity.BuyGoodsActivity
import com.example.mcdonaldapplication.databinding.FragmentHomeBinding
import com.example.mcdonaldapplication.info.BannerImageBean
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder

class HomeFragment : Fragment() {
    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentHomeBinding.inflate(inflater, container, false)

        getUser()

        binding.ccTakeaway.setOnClickListener{
            //判断是否登录
            if(!getUserLogin()){
                val navController = findNavController()
                navController.navigate(R.id.action_navigation_home_to_loginActivity)
            }
            else{
                val navController = findNavController()
                navController.navigate(R.id.action_navigation_home_to_choseAddressActivity)
            }
        }

        binding.ccOrder.setOnClickListener {
            //判断是否登录
            if(!getUserLogin()){
                val navController = findNavController()
                navController.navigate(R.id.action_navigation_home_to_loginActivity)
            }
            else{
                val navController = findNavController()
                navController.navigate(R.id.action_navigation_home_to_orderActivity)
            }
        }
        binding.tvToInt.setOnClickListener{
            val navController = findNavController()
            navController.navigate(R.id.action_navigation_home_to_integralExchangeActivity)
        }

        binding.ccMap.setOnClickListener{
            val navController = findNavController()
            navController.navigate(R.id.action_navigation_home_to_mapActivity)
        }

        binding.image1.setOnClickListener {
            val intent = Intent(context, BuyGoodsActivity::class.java)
            intent.putExtra("gid",5)
            startActivity(intent)
        }

        binding.image2.setOnClickListener {
            val intent = Intent(context, BuyGoodsActivity::class.java)
            intent.putExtra("gid",6)
            startActivity(intent)
        }

        binding.image3.setOnClickListener {
            val intent = Intent(context, BuyGoodsActivity::class.java)
            intent.putExtra("gid",7)
            startActivity(intent)
        }

        binding.image4.setOnClickListener {
            val intent = Intent(context, BuyGoodsActivity::class.java)
            intent.putExtra("gid",8)
            startActivity(intent)
        }

        val banner: Banner<BannerImageBean, BannerImageAdapter<BannerImageBean>> = binding.banner2 as Banner<BannerImageBean, BannerImageAdapter<BannerImageBean>>
        banner.setAdapter(object: BannerImageAdapter<BannerImageBean>(BannerImageBean.testData1) {
            override fun onBindView(holder: BannerImageHolder, data: BannerImageBean, position: Int, size: Int) {
                //图片加载自己实现
                Glide.with(holder.itemView)
                    .load(data.imageRes)
                    .apply(RequestOptions.bitmapTransform(RoundedCorners(30)))
                    .into(holder.imageView) }
        }).addBannerLifecycleObserver(this).indicator

        return binding.root
    }

    private fun getUser() {
        val prefs = requireActivity().getSharedPreferences("user", Context.MODE_PRIVATE)
        val integral = prefs.getInt("integral", 0).toString()
        binding.tvIntegral.text = integral
    }
    private fun getUserLogin(): Boolean {
        val prefs = requireActivity().getSharedPreferences("user", Context.MODE_PRIVATE)
        val username = prefs.getString("username", "")
        if(username != "")
            return true
        else
            return false
    }
}